﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using IBatisNet.DataMapper;
using IBatisNet.Common.Exceptions;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope;

namespace Ecreate.Data.MapperDao
{
    [Serializable]
    public class MapDao : Ecreate.Data.MapperDao.BaseDao, Ecreate.Data.IFace.IIBatisService
    {



        public ISqlMapper getIMapper()
        {

            return (ISqlMapper)GetLocalSqlMap();

        }
        public T QueryForEntity<T>(string statementName, object parameterObject)
        {

            return getIMapper().QueryForObject<T>(statementName, parameterObject);
        }


        public object Insert(string statementName, object parameterObject)
        {
            SqlMapper sqlMap = GetLocalSqlMap();
            return sqlMap.Insert(statementName, parameterObject);
        }

        public object Update(string statementName, object parameterObject)
        {
            SqlMapper sqlMap = GetLocalSqlMap();
            return sqlMap.Update(statementName, parameterObject);

        }
        public object Delete(string statementName, object parameterObject)
        {
            SqlMapper sqlMap = GetLocalSqlMap();
            return sqlMap.Delete(statementName, parameterObject);
        }

        public IList ExecuteQueryForList(string statementName, object parameterObject)
        {
            SqlMapper sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList(statementName, parameterObject);
            }
            catch (Exception e)
            {
                throw new IBatisNetException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }
        }

        public IList<T> ForList<T>(string statementName, object parameterObject)
        {

            SqlMapper sqlMap = GetLocalSqlMap();
            try
            {
                return sqlMap.QueryForList<T>(statementName, parameterObject);
            }
            catch (Exception e)
            {
                throw new IBatisNetException("Error executing query '" + statementName + "' for list.  Cause: " + e.Message, e);
            }

        }




        public string GetSql(string statementName, object paramObject)
        {
            SqlMapper sqlMap = GetLocalSqlMap();
            IMappedStatement statement = sqlMap.GetMappedStatement(statementName);
            RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, sqlMap.LocalSession);
            return scope.PreparedStatement.PreparedSql;

        }


        public object QueryForObject(string statementName, object parameterObject)
        {
            SqlMapper sqlMap = GetLocalSqlMap();
            return sqlMap.QueryForObject(statementName, parameterObject);

        }


    }
}
